require('GameData');

var MOMENT_MOVING = 20;
var MOMENT_CAPTURE = 6;
var MOMENT_FINISH = 2;

cc.Class({
    extends: cc.Component,

    properties: {

    },

    onLoad() {
        GLOBAL.WIN_WIDTH = this.node.width;
        GLOBAL.WIN_HEIGHT = this.node.height; 

        this.menu = this.node.find('GameMenu', 'GameMenu');
        this.logic = this.node.find('GameLogic', 'GameLogic');

        this.bindEvents();
        this.countTimer = 0;
        this.scheduleCbk = null;
    },

    start() {
        cc.GameProxy.getAccessToken(8, () => this.onUserLogin());
    },

    checkTimer(dt) {
        if(this.countTimer >= MOMENT_MOVING) {
            this.menu.onTiktok(this.countTimer - MOMENT_MOVING);
        }
        if(dt > 4) {
            if(this.countTimer - dt < MOMENT_MOVING) {
                this.unschedule(this.scheduleCbk, this);
                this.scheduleCbk = null;
                cc.game.restart();
            }
        }
        this.countTimer -= Math.round(dt);
    },

    bindEvents() {
        this.menu.node.on('MOVE_RESTART', () => {
            this.logic.showMotors(false);
            cc.Linker(URLS.GameState).request(data => this.onGameStart(data));
        }, this);
        this.menu.node.on('MOVE_READY', () => this.syncGameResult(() => {
            this.menu.showBetArea(false);
            this.logic.onMoveReady();
        }), this);
        this.logic.node.on('MOVE_FINISH', () => this.syncPlayerResult(() => {
            cc.Linker(URLS.GameState).request(data => this.onGameFinish(data));
        }), this);
    },

    onUserLogin() {
        cc.Linker(URLS.MotorsInfo).request(data => GLOBAL.motorsInfo = data);
        cc.Linker(URLS.GameState).request(data => this.onGameStart(data));
        if(LINK.accessToken) {
            cc.GameProxy.getUserInfo(data => {
                LINK.userInfo = data;
                this.menu.updateUserInfo();
            });
            cc.Linker(URLS.FirstLoad, {
                gameType: LINK.gameType,
                buryingType: 8
            }).showMessage(false).showLoading(false)
            .request(data => GLOBAL.IS_NEW_PLAYER = data);
        }
    },

    onGameStart(data) {
        if(this.scheduleCbk) return;
        GLOBAL.gameState = data;
        this.countTimer = data.timer;
        this.scheduleCbk = this.scheduleTime(this.checkTimer, 1);
        if(data.timer >= MOMENT_MOVING) {
            this.menu.onPlayerBet(data.timer - MOMENT_MOVING);
        } else if (data.timer > MOMENT_CAPTURE) {
            this.syncGameResult(() => this.syncPlayerResult(() => {
                this.menu.updatePhase();
                this.menu.showBetArea(false);
                this.logic.onMoveStart(MOMENT_MOVING - data.timer);
            }));
        } else {
            this.syncGameResult(() => this.syncPlayerResult(() => {
                this.menu.updatePhase();
                this.onGameFinish(data);
            }));
        }
    },

    onGameFinish(data) {
        this.countTimer = data.timer > MOMENT_MOVING ? 3 : data.timer;
        this.unschedule(this.scheduleCbk, this);
        this.scheduleCbk = null;
        this.menu.onMoveFinish(this.countTimer);
    },

    syncGameResult(cbk) {
        var count = 0;
        var noCbk = msg => {
            count++;
            if(count > 9) cc.Toast.message(msg);
            else this.scheduleOnce(() => this.getGameResult(cbk, noCbk), 0.3);
        }
        this.getGameResult(cbk, noCbk)
    },

    syncPlayerResult(cbk) {
        if(!LINK.userInfo) {
            cbk && cbk();
            return;
        }
        var count = 0;
        var noCbk = msg => {
            count++;
            if(count > 9) cc.Toast.message(msg);
            else this.scheduleOnce(() => this.getPlayerResult(cbk, noCbk), 0.3);
        }
        this.getPlayerResult(cbk, noCbk);
    },

    getGameResult(okCbk, noCbk) {
        cc.Linker(URLS.GameResult, {
            gameId: GLOBAL.gameState.gameId
        }).showLoading(false)
        .showMessage(false)
        .request(data => {
            GLOBAL.gameResult = data;
            okCbk && okCbk();
        }, noCbk);
    },

    getPlayerResult(okCbk, noCbk) {
        cc.Linker(URLS.PlayerResult, {
            gameId: GLOBAL.gameState.gameId
        }).showLoading(false)
        .showMessage(false)
        .request(data => {
            GLOBAL.playerResult = data;
            okCbk && okCbk();
        }, noCbk);
    },

});
